<?php
class mission_mdl extends CI_Model
{

	
	public function get_all($limit, $start)
	{
$this->load->library('session');
$year=$this->session->userdata('annee');

	$query = $this->db->query("SELECT
mission.id_mission,
mission.numero,
mission.annee,
mission.nom,
mission.grade,
mission.normale,
mission.nuit,
mission.transport,
mission.id_engagement_fk,
mission.montant
FROM
mission
WHERE
mission.annee = $year
LIMIT  $start ,$limit

 ");
	
return $query->result();
     

	
	}
	
	 public function record_count_all() {

$this->load->library('session');
$year=$this->session->userdata('annee');
	  	
      $q = $this->db->query("SELECT
mission.id_mission
FROM
mission
WHERE
mission.annee = $year



");

$cont=$q->num_rows();
return $cont;

    }
	
	
	public function get_all_per_nom()
	{

$this->load->library('session');
$year=$this->session->userdata('annee');

	$query = $this->db->query("SELECT
fournisseurs.nom ,
fourn_article.id_fournisseur_fk,
fourn_article.id_article_fk,
fournisseurs.id_fournisseur,
articles.nom AS article,
bcs.id_bc,
bcs.montant,
bcs.date
FROM
fournisseurs
INNER JOIN fourn_article ON fourn_article.id_fournisseur_fk = fournisseurs.id_fournisseur
INNER JOIN articles ON fourn_article.id_article_fk = articles.id_article
INNER JOIN bcs ON bcs.id_fournisseur_fk = fournisseurs.id_fournisseur
WHERE bcs.annee = $year
GROUP BY
fournisseurs.id_fournisseur

 ");
	
return $query->result();
     

	
	}
	
	
		public function count_per_nom_fourn($nom)
	{
$this->load->library('session');
$year=$this->session->userdata('annee');

	$query = $this->db->query("SELECT
fournisseurs.nom AS fournisseur,
fourn_article.id_fournisseur_fk,
fourn_article.id_article_fk,
fournisseurs.id_fournisseur,
articles.nom AS article,
bcs.id_bc,
bcs.date
FROM
fournisseurs
INNER JOIN fourn_article ON fourn_article.id_fournisseur_fk = fournisseurs.id_fournisseur
INNER JOIN articles ON fourn_article.id_article_fk = articles.id_article
INNER JOIN bcs ON bcs.id_fournisseur_fk = fournisseurs.id_fournisseur
WHERE
fournisseurs.nom  like '%$nom%' AND
bcs.annee = $year
 ");
$cont=$query->num_rows();
return $cont;
	}
		public function get_all2($limit, $start,$xx)
	{
$this->load->library('session');
$year=$this->session->userdata('annee');

	$query = $this->db->query("SELECT
fournisseurs.nom AS fournisseur,
fournisseurs.id_fournisseur,
bcs.id_bc,
bcs.date,
bcs.montant,
bcs.id_fournisseur_fk,
bcs.numero,
engagements.numero AS num_engagement,
mandats.numero AS num_mondat,
articles.id_article,
articles.num_article,
articles.nom AS article
FROM
fournisseurs
INNER JOIN bcs ON bcs.id_fournisseur_fk = fournisseurs.id_fournisseur
LEFT JOIN engagements ON bcs.id_engagement_fk = engagements.id_engagement
LEFT JOIN mandats ON bcs.id_mondat_fk = mandats.id_mandat
INNER JOIN articles ON bcs.id_article_fk = articles.id_article

WHERE
articles.id_article = $xx AND
bcs.annee = $year
LIMIT  $start ,$limit

 ");
	
return $query->result();

	}
	

 
    public function ajout_mission($nom,$numero,$grade,$date,$transport,$normale,$nuit,$montant)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 
	$query = $this->db->query("INSERT INTO `mission` (`annee`, `nom`, `grade`, `normale`, `nuit`, `transport`, `numero`, `montant`, `date`) VALUES ('$year', '$nom', '$grade', '$normale', '$nuit', '$transport','$numero','$montant','$date')
 ");
   

	
	}
	
	  public function update_mission($id_mission,$nom,$numero,$grade,$date,$transport,$normale,$nuit,$montant)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 
	$query = $this->db->query("UPDATE `mission` SET `numero`='$numero', `nom`='$nom', `grade`='$grade', `normale`='$normale', `nuit`='$nuit', `transport`='$transport', `montant`='$montant' WHERE (`id_mission`='$id_mission') ");
   

	
	}
	
		  public function engager_mission($id_mission,$id_engagement)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 
	$query = $this->db->query("UPDATE `mission` SET `id_engagement_fk`='$id_engagement' WHERE (`id_mission`='$id_mission') ");
   

	
	}
	
	  public function get_numero()
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 
	$query = $this->db->query("SELECT
mission.numero
FROM
mission
ORDER BY
mission.numero DESC
LIMIT 0, 1

 ");
   
return $query->result();

	
	}

       public function get_detaille($id_mission)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 
	$query = $this->db->query("SELECT
*
FROM
mission
WHERE
mission.id_mission=$id_mission

 ");
   
return $query->result();

	
	}
	
		  public function existe_numero($numero)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 
	$query = $this->db->query("SELECT
mission.numero
FROM
mission
WHERE
mission.numero = $numero

 ");
   
return $query->result();

	
	}
}


?>